package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("add")
    public Result add(@RequestBody Map map){

        try {

            //获取用户提交的token
            String token = (String) map.get("token");
            //获取redis中的token
            String redisToken = jedisPool.getResource().get(token);
            if (redisToken ==null || !redisToken.equals(token)){
                return new Result(false,"非法请求");
            }

            //将redis中的令牌删除
            jedisPool.getResource().del(token);
            //通过map获取电话号码
            String telephone = (String) map.get("telephone");

            //设置获取验证码的key
            String key = telephone+ RedisMessageConstant.SENDTYPE_ORDER;

            //从redis中获取发送给用户的验证码
            String code = jedisPool.getResource().get(key);

            //获取用户提交的验证码
            String validateCode = (String) map.get("validateCode");

            //对用户提交的验证码进行校验
            if (code ==null || !code.equals(validateCode)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }else{
                //执行新增，获取订单信息
                Order order = orderService.add(map);

                //发送短信通知用户
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, ValidateCodeUtils.generateValidateCode(4).toString());

                return new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order);

            }
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ORDER_FAIL);

        }


    }

    /**
     * 发送令牌
     */
    @RequestMapping("token")
    public Result token(){
        //生成一个唯一的令牌
        String token = UUID.randomUUID().toString();
        //将令牌保存到redis中
        jedisPool.getResource().setex(token,60*10,token);

        return new Result(true,"令牌发送成功",token);

    }

    /**
     * 通过订单id查询订单相关信息
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){

        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
